package com.cloudpivot.project.crmprojectnew.controller;

import com.cloudpivot.project.crmprojectnew.pojo.ResultJson;
import com.cloudpivot.project.crmprojectnew.pojo.newproduct.NewProduct;
import com.cloudpivot.project.crmprojectnew.pojo.newproduct.NewProductList;
import com.cloudpivot.project.crmprojectnew.service.impl.NewProductServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.*;

/**
 * 设备\产品 crm 中台接口
 */
@Controller
@Slf4j
@RequestMapping("/crmNewProduct")
public class NewProductController {

    @Autowired
    private NewProductServiceImpl newProductService;

    /**
     * 查询 设备/产品信息
     */
    @GetMapping("/getInfo")
    @ResponseBody
    public NewProduct getNewProduct(@RequestParam(value = "guid") String guid) {
        log.info("获取客户设备-产品名称");
        return newProductService.getNewProduct(guid);

    }

    /**
     * 查询 设备/产品列表
     *
     * @param userid
     * @return
     */
    @GetMapping("/getInfos")
    @ResponseBody
    public NewProductList getNewProducts(@RequestParam(value = "userid") String userid, @RequestParam(value = "id") String id, @RequestParam(value = "pageIndex") int pageIndex, @RequestParam(value = "pageSize") int pageSize) {
        log.info("获取客户设备-产品名称-List: " + userid);
        log.info("获取客户设备-产品名称-id:" + id);
        log.info("获取分页信息 pageIndex:" + pageIndex+" pageSize:"+pageSize);
        if (!"".equals(id)) {
            return newProductService.getNewProducts(userid, id);
        } else {
            return newProductService.getNewProducts(userid,pageIndex,pageSize);
        }


    }

    @ResponseBody
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ResultJson exceptionHandler(MethodArgumentNotValidException e) {
        ResultJson resultJson = new ResultJson(-1, "error");
        resultJson.setMsg(e.getBindingResult().getFieldError().getDefaultMessage());
        return resultJson;
    }
}
